<script type="text/javascript">
$(document).ready(function() {
    //datepicker
	$('[datepicker="datepicker"]').each(function(){
		$(this).datepicker({
			  dateFormat: 'yy-mm-dd'
			, minDate: new Date(new Date().getTime() )
			, onClose: function(dateText, inst) {
				$('[name="'+$(this).attr('target') + '_year"]').val(inst.selectedYear)
				$('[name="'+$(this).attr('target') + '_month"]').val(inst.selectedMonth + 1)
				$('[name="'+$(this).attr('target') + '_day"]').val(inst.selectedDay)
			}
		}).attr("readonly", true)
    });
	$("#makerInput").autocomplete({
        minLength: 0,
        source: function(request, response) {
           $.ajax({
                url: "${createLink(controller:'purchaseAjax', action: 'staffInstanceList')}",
                data: { term: request.term },
                dataType: "json",
                type: "POST",
                success: function( data ) {
                    response( $.map( data, function( item ) {
                        return{
                            id : item[0],
                            value: item[1]+'-'+item[2],
                            label: item[1]+'-'+item[2]
                        }
                    }));
                }
            });
        },
        select: function( event, ui ) {
            $("#maker\\.id").val(ui.item.id);
        }
    });
    //dealer
	$("#dealerInput").autocomplete({
        minLength: 0,
        source: function(request, response) {
           $.ajax({
                url: "${createLink(controller:'purchaseAjax', action: 'dealerInstanceList')}",
                data: { term: request.term },
                dataType: "json",
                type: "POST",
                success: function( data ) {
                    response( $.map( data, function( item ) {
                        //[$recordType.name]$serialNumber-$name
                        return{
                            id : item[0],
                            value: item[1]+'-'+item[2],
                            label: item[1]+'-'+item[2]
                        }
                    }));
                }
            });
        },
        select: function( event, ui ) {
            $("#dealer\\.id").val(ui.item.id);
        }
    });
    //director
	$("#directorInput").autocomplete({
        minLength: 0,
        source: function(request, response) {
           $.ajax({
                url: "${createLink(controller:'purchaseAjax', action: 'staffInstanceList')}",
                data: { term: request.term },
                dataType: "json",
                type: "POST",
                success: function( data ) {
                    response( $.map( data, function( item ) {
                        return{
                            id : item[0],
                            value: item[1]+'-'+item[2],
                            label: item[1]+'-'+item[2]
                        }
                    }));
                }
            });
        },
        select: function( event, ui ) {
            $("#director\\.id").val(ui.item.id);
        }
    });
});
</script>
